package com.lzx.iteam;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.*;
import android.view.inputmethod.InputMethodManager;
import android.widget.*;
import com.lzx.iteam.base.FindViewById;
import com.lzx.iteam.base.MyBaseActivity;
import com.lzx.iteam.bean.AttendanceClassData;
import com.lzx.iteam.net.AsynHttpClient;
import com.lzx.iteam.net.GetMsgHttpReceiver;
import com.lzx.iteam.net.LocalLogin;
import com.lzx.iteam.util.StringUtil;
import com.lzx.iteam.widget.selectdate.WheelView;
import com.lzx.iteam.widget.selectdate.adapter.NumericWheelAdapter;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


/**
 * Created with IntelliJ IDEA.
 * **********************************
 * User: zhangshuai
 * Date: 2016年 11月 24日
 * Time: 下午5:05
 *
 * @QQ : 1234567890
 * **********************************
 *
 * 班次内容设置页面
 */
public class AttendanceSubClassEditActivity extends MyBaseActivity implements View.OnClickListener{

    @FindViewById(id = R.id.title_ll_back)
    private LinearLayout mLayoutBack;
    @FindViewById(id = R.id.title_tv_center)
    private TextView mTvTitle;
    @FindViewById(id = R.id.title_tv_right)
    private TextView mTvRight;

    @FindViewById(id = R.id.et_attendance_sub_name)
    private TextView mTvSubName;
    @FindViewById(id = R.id.tv_attendance_setting_starttime1)
    private TextView mTvStartTime1;
    @FindViewById(id = R.id.tv_attendance_setting_endtime1)
    private TextView mTvEndTime1;
    @FindViewById(id = R.id.tv_attendance_setting_starttime2)
    private TextView mTvStartTime2;
    @FindViewById(id = R.id.tv_attendance_setting_endtime2)
    private TextView mTvEndTime2;
    @FindViewById(id = R.id.tv_attendance_setting_starttime_text1)
    private TextView mTvStartTimeText1;
    @FindViewById(id = R.id.tv_attendance_setting_endtime_text1)
    private TextView mTvEndTimeText1;
    @FindViewById(id = R.id.tv_attendance_setting_starttime_text2)
    private TextView mTvStartTimeText2;
    @FindViewById(id = R.id.tv_attendance_setting_endtime_text2)
    private TextView mTvEndTimeText2;

    @FindViewById(id = R.id.tv_attendance_setting_type_one)
    private TextView mTvTypeOne;
    @FindViewById(id = R.id.tv_attendance_setting_type_two)
    private TextView mTvTypeTwo;
    @FindViewById(id = R.id.v_attendance_setting_type_line)
    private View mTypeLine;

    @FindViewById(id = R.id.rl_attendance_setting_starttime1)
    private RelativeLayout mRlStartTime1;
    @FindViewById(id = R.id.rl_attendance_setting_endtime1)
    private RelativeLayout mRlEndTime1;
    @FindViewById(id = R.id.rl_attendance_setting_starttime2)
    private RelativeLayout mRlStartTime2;
    @FindViewById(id = R.id.rl_attendance_setting_endtime2)
    private RelativeLayout mRlEndTime2;
    @FindViewById(id = R.id.rl_attendance_setting_workdate)
    private RelativeLayout mRlWorkDate;


    private AttendanceClassData mAttendanceClassData;
    private ArrayList<AttendanceClassData> mSubClasses;
    private PopupWindow mTimePop;
    private WheelView hour;
    private WheelView mins;

    private int mStartHour1 = 9, mEndHour1 = 12, mStartHour2 = 13, mEndHour2 = 18;
    private int mStartMinute1 = 0, mEndMinute1 = 0, mStartMinute2 = 30, mEndMinute2 = 0;
    private String mStartTime1 = "9:00", mEndTime1 = "12:00";
    private String mStartTime2 = "13:30", mEndTime2 = "18:00";
    private String mSubName;
    private int mIsQuartic;  //0：一天一次上下班打卡，1：一天两次上下班打卡
    private int mFlagIsNew;  // 0:考勤不存在   1: 考勤已经存在
    private int mFlagCreateOrEdit;  // 0:创建   1: 编辑
    private int mPosition;   // 编辑的 position
    private String mGroupId;
    private String mFormId;
    private String mSubId;

    private final int CREATE_EDIT_SUB = 30000;

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
//            super.handleMessage(msg);
            switch (msg.what){
                case CREATE_EDIT_SUB:
                    waitDlgDismiss();
                    if (msg.arg1 == 0){
                        if (StringUtil.isEmpty(mSubId)){
                            try {
                                JSONObject object = (JSONObject) msg.obj;
                                JSONObject data = object.getJSONObject("data");
                                String subId = data.getString("sub_id");
                                mAttendanceClassData.set_id(subId);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                        backActivity();

                    }else {
                        Toast.makeText(mActivity, (String) msg.obj, Toast.LENGTH_LONG).show();
                    }
                    break;

            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState,R.layout.attendance_shift_edit_layout);
    }

    @Override
    public void initListener() {
        mLayoutBack.setOnClickListener(this);
        mTvRight.setOnClickListener(this);
        mRlStartTime1.setOnClickListener(this);
        mRlStartTime2.setOnClickListener(this);
        mRlEndTime1.setOnClickListener(this);
        mRlEndTime2.setOnClickListener(this);
        mTvTypeOne.setOnClickListener(this);
        mTvTypeTwo.setOnClickListener(this);
    }

    @Override
    public void initData() {
        mTvTitle.setText("班次设置");
        mTvRight.setText("保存");
        mTvEndTimeText1.setText("下班时间");
        mTvStartTimeText2.setText("上班时间");
        mRlWorkDate.setVisibility(View.GONE);
        Intent intent = getIntent();
        mGroupId = intent.getStringExtra("group_id");
        mFormId = intent.getStringExtra("form_id");
        mPosition = intent.getIntExtra("position",-1);
        if (!StringUtil.isEmpty(mFormId)){
            mFlagIsNew = 1;
        }
        mSubClasses = (ArrayList<AttendanceClassData>) intent.getSerializableExtra("sub_classes");
        mAttendanceClassData = (AttendanceClassData) intent.getSerializableExtra("sub_class");
        if (mAttendanceClassData == null) {
            mFlagCreateOrEdit = 0;
        }else{
            mFlagCreateOrEdit = 1;
            mSubId = mAttendanceClassData.get_id();
            mSubName = mAttendanceClassData.getSub_name();
            mTvSubName.setText(mSubName);
            mIsQuartic = mAttendanceClassData.getIs_quartic();

            mStartTime1 = mAttendanceClassData.getStart_work_time();
            mStartHour1 = Integer.parseInt(mStartTime1.split(":")[0]);
            mStartMinute1 = Integer.parseInt(mStartTime1.split(":")[1]);
            mTvStartTime1.setText(mStartTime1);

            mEndTime2 = mAttendanceClassData.getEnd_work_time();
            mEndHour2 = Integer.parseInt(mEndTime2.replace("-","").split(":")[0]);
            mEndMinute2 = Integer.parseInt(mEndTime2.replace("-","").split(":")[1]);
            if (mEndTime2.contains("-")){
                mTvEndTime2.setText(mEndTime2.replace("-","次日"));
            }else{
                mTvEndTime2.setText(mEndTime2);
            }
            if (mIsQuartic == 1){
                mEndTime1 = mAttendanceClassData.getNoon_end_time();
                mEndHour1 = Integer.parseInt(mEndTime1.replace("-","").split(":")[0]);
                mEndMinute1 = Integer.parseInt(mEndTime1.replace("-","").split(":")[1]);

                if (mEndTime1.contains("-")){
                    mTvEndTime1.setText(mEndTime1.replace("-","次日"));
                } else{
                   mTvEndTime1.setText(mEndTime1);
                }

                mStartTime2 = mAttendanceClassData.getNoon_start_time();
                mStartHour2 = Integer.parseInt(mStartTime2.replace("-","").split(":")[0]);
                mStartMinute2 = Integer.parseInt(mStartTime2.replace("-","").split(":")[1]);

                if (mStartTime2.contains("-")){
                    mTvStartTime2.setText(mStartTime2.replace("-","次日"));
                } else{
                    mTvStartTime2.setText(mStartTime2);
                }

                updateTypeView(false);

            }

        }
    }

    /**
     * 选择时间
     *
     * @param type 1：上午上班时间 2：上午下班时间 3：下午上班时间 4：下午下班时间
     */
    public void showTimeSelectPop(final int type) {

        try {
            ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(mActivity.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        final View view = LayoutInflater.from(this).inflate(R.layout.select_time_pop_layout, null);
        hour = (WheelView) view.findViewById(R.id.new_hour);
        initHour();
        mins = (WheelView) view.findViewById(R.id.new_mins);
        initMins();
        // 设置当前时间
        if (type == 1) {
            hour.setCurrentItem(mStartHour1);
            mins.setCurrentItem(mStartMinute1);
        } else if (type == 2) {
            hour.setCurrentItem(mEndHour1);
            mins.setCurrentItem(mEndMinute1);
        } else if (type == 3) {
            hour.setCurrentItem(mStartHour2);
            mins.setCurrentItem(mStartMinute2);
        } else if (type == 4) {
            hour.setCurrentItem(mEndHour2);
            mins.setCurrentItem(mEndMinute2);
        }


        TextView bt = (TextView) view.findViewById(R.id.new_set);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (type == 1) {
                    if (mIsQuartic == 0) {
                        if (!mEndTime2.contains("-") && !compareNextTime(hour.getCurrentItem(), mins.getCurrentItem(), mEndHour2, mEndMinute2)){
                            Toast.makeText(AttendanceSubClassEditActivity.this, "当前时间不能大于下班时间", Toast.LENGTH_LONG).show();
                            return;
                        } else{
                            mStartHour1 = hour.getCurrentItem();
                            mStartMinute1 = mins.getCurrentItem();
                            mStartTime1 = String.format("%02d:%02d",mStartHour1,mStartMinute1);
                            mTvStartTime1.setText(mStartTime1);
                        }
                    } else {
                        if (!mEndTime1.contains("-") && !compareNextTime(hour.getCurrentItem(), mins.getCurrentItem(), mEndHour1, mEndMinute1)){
                            Toast.makeText(AttendanceSubClassEditActivity.this, "当前时间不能大于下班时间", Toast.LENGTH_LONG).show();
                            return;
                        } else{
                            mStartHour1 = hour.getCurrentItem();
                            mStartMinute1 = mins.getCurrentItem();
                            mStartTime1 = String.format("%02d:%02d",mStartHour1,mStartMinute1);
                            mTvStartTime1.setText(mStartTime1);
                        }
                    }
                } else if (type == 2) {
                    if (mStartTime2.contains("-")){  // 上班2 时间是次日
                        mEndHour1 = hour.getCurrentItem();
                        mEndMinute1 = mins.getCurrentItem();
                        mEndTime1 = String.format("%02d:%02d",mEndHour1,mEndMinute1);
                        if (compareNextTime(hour.getCurrentItem(), mins.getCurrentItem(), mStartHour2, mStartMinute2)) {
                            mEndTime1 = "-" + mEndTime1;
                        }
                    } else{ // 上班2 时间是当日
                        if (!compareLastTime(hour.getCurrentItem(), mins.getCurrentItem(), mStartHour1, mStartMinute1)) {
                            Toast.makeText(AttendanceSubClassEditActivity.this, "当前时间不能小于上班1 时间", Toast.LENGTH_LONG).show();
                            return;
                        }else if (!compareNextTime(hour.getCurrentItem(), mins.getCurrentItem(), mStartHour2, mStartMinute2)) {
                            Toast.makeText(AttendanceSubClassEditActivity.this, "当前时间不能大于上班2 时间", Toast.LENGTH_LONG).show();
                            return;
                        }  else {
                            mEndHour1 = hour.getCurrentItem();
                            mEndMinute1 = mins.getCurrentItem();
                            mEndTime1 = String.format("%02d:%02d",mEndHour1,mEndMinute1);
                        }
                    }
                    if (mEndTime1.contains("-")){
                        mTvEndTime1.setText(mEndTime1.replace("-","次日 "));
                    }  else{
                        mTvEndTime1.setText(mEndTime1);
                    }

                } else if (type == 3) {

                    if (mEndTime1.contains("-")){
                        if (!compareNextTime(hour.getCurrentItem(), mins.getCurrentItem(), mEndHour2, mEndMinute2)) {
                            Toast.makeText(AttendanceSubClassEditActivity.this, "当前时间不能大于 下班2 时间", Toast.LENGTH_LONG).show();
                            return;
                        } else if (!compareLastTime(hour.getCurrentItem(), mins.getCurrentItem(), mEndHour1, mEndMinute1)) {
                            Toast.makeText(AttendanceSubClassEditActivity.this, "当前时间不能小于 下班1 时间", Toast.LENGTH_LONG).show();
                            return;
                        }else{
                            mStartHour2 = hour.getCurrentItem();
                            mStartMinute2 = mins.getCurrentItem();
                            mStartTime2 = "-" + String.format("%02d:%02d",mStartHour2,mStartMinute2);
                        }

                    }else{
                        if (mEndTime2.contains("-")){
                            if (!compareLastTime(hour.getCurrentItem(), mins.getCurrentItem(), mEndHour1, mEndMinute1)) {
                                if (compareNextTime(hour.getCurrentItem(),mins.getCurrentItem(),mEndHour2,mEndMinute2)){
                                    mStartHour2 = hour.getCurrentItem();
                                    mStartMinute2 = mins.getCurrentItem();
                                    mStartTime2 = "-" + String.format("%02d:%02d",mStartHour2,mStartMinute2);
                                }else{
                                    Toast.makeText(AttendanceSubClassEditActivity.this, "当前时间不能小于 下班1 时间", Toast.LENGTH_LONG).show();
                                    return;
                                }
                            }else{
                                mStartHour2 = hour.getCurrentItem();
                                mStartMinute2 = mins.getCurrentItem();
                                mStartTime2 = String.format("%02d:%02d",mStartHour2,mStartMinute2);
                                if (compareNextTime(hour.getCurrentItem(), mins.getCurrentItem(), mEndHour2, mEndMinute2)) {
                                    mStartTime2 = "-" + mStartTime2;
                                }
                            }
                        }else{
                            if (!compareNextTime(hour.getCurrentItem(), mins.getCurrentItem(), mEndHour2, mEndMinute2)) {
                                Toast.makeText(AttendanceSubClassEditActivity.this, "当前时间不能大于下午下班时间", Toast.LENGTH_LONG).show();
                                return;
                            } else if (!compareLastTime(hour.getCurrentItem(), mins.getCurrentItem(), mEndHour1, mEndMinute1)) {
                                Toast.makeText(AttendanceSubClassEditActivity.this, "当前时间不能小于上午下班时间", Toast.LENGTH_LONG).show();
                                return;
                            }else{
                                mStartHour2 = hour.getCurrentItem();
                                mStartMinute2 = mins.getCurrentItem();
                                mStartTime2 = String.format("%02d:%02d",mStartHour2,mStartMinute2);
                            }
                        }
                    }
                    if (mStartTime2.contains("-")){
                        mTvStartTime2.setText(mStartTime2.replace("-","次日 "));
                    }else{
                        mTvStartTime2.setText(mStartTime2);
                    }
                } else if (type == 4) {
                    if (mIsQuartic == 0) {
                        mEndHour2 = hour.getCurrentItem();
                        mEndMinute2 = mins.getCurrentItem();
                        mEndTime2 = String.format("%02d:%02d",mEndHour2,mEndMinute2);
                        if (compareLastTime(hour.getCurrentItem(), mins.getCurrentItem(), mStartHour1, mStartMinute1)) {
                            mTvEndTime2.setText(mEndTime2);
                        } else {
                            mEndTime2 = "-" + mEndTime2;
                            mTvEndTime2.setText(mEndTime2.replace("-","次日 "));
                        }
                    } else {
                        if (mStartTime2.contains("-")){
                            if (compareLastTime(hour.getCurrentItem(), mins.getCurrentItem(), mStartHour2, mStartMinute2)) {
                                mEndHour2 = hour.getCurrentItem();
                                mEndMinute2 = mins.getCurrentItem();
                                mEndTime2 = "-" + String.format("%02d:%02d",mEndHour2,mEndMinute2);;
                                mTvEndTime2.setText(mEndTime2.replace("-","次日 "));
                            } else {
                            Toast.makeText(AttendanceSubClassEditActivity.this, "当前时间不能小于下午上班时间", Toast.LENGTH_LONG).show();
                            return;
                            }

                        } else{
                            mEndHour2 = hour.getCurrentItem();
                            mEndMinute2 = mins.getCurrentItem();
                            mEndTime2 = String.format("%02d:%02d",mEndHour2,mEndMinute2);
                            if (compareLastTime(hour.getCurrentItem(), mins.getCurrentItem(), mStartHour2, mStartMinute2)) {
                                mTvEndTime2.setText(mEndTime2);
                            } else {
                                mEndTime2 = "-" + mEndTime2;
                                mTvEndTime2.setText(mEndTime2.replace("-","次日 "));
                            }
                        }
                    }

                }
                mTimePop.dismiss();
            }
        });
        TextView cancel = (TextView) view.findViewById(R.id.new_cancel);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mTimePop.dismiss();
            }
        });

        mTimePop = new PopupWindow(this);
        mTimePop.setBackgroundDrawable(new BitmapDrawable());
        mTimePop.setFocusable(true);
        mTimePop.setTouchable(true);
        mTimePop.setOutsideTouchable(true);
        mTimePop.setContentView(view);
        mTimePop.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        mTimePop.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        mTimePop.setAnimationStyle(R.style.bottomStyle);
        mTimePop.showAtLocation(view, Gravity.BOTTOM, 0, 0);
        mTimePop.update();
        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                mTimePop.dismiss();
                return false;
            }
        });
    }


    /**
     * 初始化时
     */
    private void initHour() {
        NumericWheelAdapter numericWheelAdapter = new NumericWheelAdapter(this, 0, 23, "%02d");
        numericWheelAdapter.setLabel(" 时");
        //		numericWheelAdapter.setTextSize(15);  设置字体大小
        hour.setViewAdapter(numericWheelAdapter);
        hour.setCyclic(true);
    }

    /**
     * 初始化分
     */
    private void initMins() {
        NumericWheelAdapter numericWheelAdapter = new NumericWheelAdapter(this, 0, 59, "%02d", 1);
        numericWheelAdapter.setLabel(" 分");
//		numericWheelAdapter.setTextSize(15);  设置字体大小
        mins.setViewAdapter(numericWheelAdapter);
        mins.setCyclic(true);
    }

    /**
     * 比下一条时间 小 ？
     *
     * @param hour
     * @param minute
     * @param hour2
     * @param minute2
     * @return
     */
    private boolean compareNextTime(int hour, int minute, int hour2, int minute2) {
        if (hour < hour2) {
            return true;
        } else if (hour == hour2) {
            if (minute < minute2) {
                return true;
            } else {
                return false;
            }
        } else if (hour > hour2) {
            return false;
        }
        return false;
    }

    /**
     * 比上一条时间比较  大 ？
     *
     * @param hour
     * @param minute
     * @param hour2
     * @param minute2
     * @return
     */
    private boolean compareLastTime(int hour, int minute, int hour2, int minute2) {
        if (hour > hour2) {
            return true;
        } else if (hour == hour2) {
            if (minute > minute2) {
                return true;
            } else {
                return false;
            }
        } else if (hour < hour2) {
            return false;
        }
        return false;
    }


    /**
     * 一次打卡 和 二次打卡 切换
     * @param flag 是否要更新数据
     */
    private void updateTypeView(boolean flag) {
        if (mIsQuartic == 0) {
            mRlEndTime1.setVisibility(View.GONE);
            mRlStartTime2.setVisibility(View.GONE);
            mTypeLine.setVisibility(View.GONE);
            mTvTypeOne.setBackgroundDrawable(getResources().getDrawable(R.drawable.attendance_type_bg1));
            mTvTypeTwo.setBackgroundDrawable(getResources().getDrawable(R.drawable.attendance_type_bg2));
            mTvTypeOne.setTextColor(getResources().getColor(R.color.orange_sign_normal));
            mTvTypeTwo.setTextColor(getResources().getColor(R.color.schedule_day_bg));

            mEndHour1 = 12; mStartHour2 = 13;
            mEndMinute1 = 0; mStartMinute2 = 30;
            mEndTime1 = "12:00";
            mStartTime2 = "13:30";
            mTvEndTime1.setText(mEndTime1);
            mTvStartTime2.setText(mStartTime2);

            if (compareLastTime(mEndHour2,mEndMinute2,mStartHour1,mStartMinute1)){
                if (mEndTime2.contains("-")){
                    mEndTime2 = mEndTime2.replace("-","");
                    mTvEndTime2.setText(mEndTime2);
                }
            }else{
                mEndTime2 = "-" + mEndTime2;
                mTvEndTime2.setText(mEndTime2.replace("-","次日 "));
            }
        } else if (mIsQuartic == 1){
            mRlEndTime1.setVisibility(View.VISIBLE);
            mRlStartTime2.setVisibility(View.VISIBLE);
            mTypeLine.setVisibility(View.VISIBLE);
            mTvTypeTwo.setBackgroundDrawable(getResources().getDrawable(R.drawable.attendance_type_bg1));
            mTvTypeOne.setBackgroundDrawable(getResources().getDrawable(R.drawable.attendance_type_bg2));
            mTvTypeOne.setTextColor(getResources().getColor(R.color.schedule_day_bg));
            mTvTypeTwo.setTextColor(getResources().getColor(R.color.orange_sign_normal));

            if (flag){

                if (compareNextTime(mStartHour1,mStartMinute1,12,0)){
                    mStartHour1 = 9; mStartMinute1 = 0;
                    mStartTime1 = String.format("%02d:%02d",mStartHour1,mStartMinute1);
                    mTvStartTime1.setText(mStartTime1);
                }

                if (compareLastTime(mEndHour2,mEndMinute2,mStartHour2,mStartMinute2)){
                    if (mEndTime2.contains("-")){
                        mEndTime2 = mEndTime2.replace("-","");
                        mTvEndTime2.setText(mEndTime2);
                    }
                }else{
                    if (!mEndTime2.contains("-")){
                        mEndTime2 = "-" + mEndTime2;
                        mTvEndTime2.setText(mEndTime2.replace("-","次日 "));
                    }
                }
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.title_ll_back:
                finish();
                break;
            case R.id.title_tv_right:
                mSubName = mTvSubName.getText().toString().trim();
                if (StringUtil.isEmpty(mSubName)){
                    Toast.makeText(AttendanceSubClassEditActivity.this, "班次名称不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (mSubClasses != null && mSubClasses.size() > 0){
                    boolean hasName = false;
                    for (int i = 0; i < mSubClasses.size(); i++) {
                        AttendanceClassData data = mSubClasses.get(i);
                        if (data.getPositon() != mPosition && data.getSub_name().equals(mSubName)){
                            hasName = true;
                            break;
                        }
                    }
                    if (hasName){
                        Toast.makeText(AttendanceSubClassEditActivity.this, "班次名称已存在", Toast.LENGTH_SHORT).show();
                        return;
                    }
                }
                if (mAttendanceClassData == null){
                    mAttendanceClassData = new AttendanceClassData();
                    mAttendanceClassData.setPositon(-1);
                }
                mAttendanceClassData.setSub_name(mSubName);
                mAttendanceClassData.setStart_work_time(mStartTime1);
                mAttendanceClassData.setEnd_work_time(mEndTime2);
                if (mIsQuartic == 0){
                    mAttendanceClassData.setIs_quartic(0);
                }else if (mIsQuartic == 1){
                    mAttendanceClassData.setIs_quartic(1);
                    mAttendanceClassData.setNoon_end_time(mEndTime1);
                    mAttendanceClassData.setNoon_start_time(mStartTime2);
                }
                if (mFlagIsNew == 0){
                   backActivity();
                }else{
                    waitDlgShow();
                    createOrEditSub();
                }
                break;
            case R.id.tv_attendance_setting_type_one:
                mIsQuartic = 0;
                updateTypeView(true);
                break;
            case R.id.tv_attendance_setting_type_two:
                mIsQuartic = 1;
                updateTypeView(true);
                break;
            case R.id.rl_attendance_setting_starttime1:
                showTimeSelectPop(1);
                break;
            case R.id.rl_attendance_setting_endtime1:
                showTimeSelectPop(2);
                break;
            case R.id.rl_attendance_setting_starttime2:
                showTimeSelectPop(3);
                break;
            case R.id.rl_attendance_setting_endtime2:
                showTimeSelectPop(4);
                break;

        }


    }

    /**
     * 返回 班次列表
     */
    private void backActivity(){

        Intent backData = new Intent();
        backData.putExtra("sub_class",mAttendanceClassData);
        setResult(RESULT_OK,backData);
        finish();

    }
    /**
     * 创建 / 编辑 排班
     */
    private void createOrEditSub(){

        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_SID, LocalLogin
              .getInstance().mSid));
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_GROUP_ID, mGroupId));
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_FORM_ID, mFormId));
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_SUB_NAME, mSubName));
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_IS_QUARTIC, mIsQuartic + ""));
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_START_WORK_TIME, mAttendanceClassData.getStart_work_time()));
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_END_WORK_TIME, mAttendanceClassData.getEnd_work_time()));

        if (mIsQuartic == 1){
            nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_NOON_END_TIME, mAttendanceClassData.getNoon_end_time()));
            nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_NOON_START_TIME, mAttendanceClassData.getNoon_start_time()));
        }
        GetMsgHttpReceiver msg = new GetMsgHttpReceiver(mHandler.obtainMessage(CREATE_EDIT_SUB));
        if (mFlagCreateOrEdit == 0){
            msg.mApi = AsynHttpClient.API_EVENT_ATTENDANCE_NEW_CLOCK;
        }else{
            nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_SUB_ID, mSubId));
            msg.mApi = AsynHttpClient.API_EVENT_ATTENDANCE_UPDATE_CLOCK;
        }
        msg.mParams = nvps;
        AsynHttpClient.getInstance(this).execEventHttp(msg);


    }

}
